package com.cxs.qrwidgedemo.widget.widget;

import com.cxs.qrwidgedemo.MainAbility;
import com.cxs.qrwidgedemo.ResourceTable;
import com.cxs.qrwidgedemo.util.QRUtils;
import com.cxs.qrwidgedemo.widget.controller.FormController;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentProvider;
import ohos.ai.cv.common.ConnectionCallback;
import ohos.ai.cv.common.VisionManager;
import ohos.app.Context;
import ohos.event.intentagent.IntentAgent;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;

import java.time.temporal.ValueRange;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class WidgetImpl extends FormController {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, WidgetImpl.class.getName());
    private static final int DEFAULT_DIMENSION_2X2 = 2;
    private static final Map<Integer, Integer> RESOURCE_ID_MAP = new HashMap<>();
    private boolean isConnected = false;

    static {
        RESOURCE_ID_MAP.put(DEFAULT_DIMENSION_2X2, ResourceTable.Layout_form_grid_pattern_widget_2_2);
    }

    public WidgetImpl(Context context, String formName, Integer dimension) {
        super(context, formName, dimension);
    }


    // 定义ConnectionCallback回调，实现连接能力引擎成功与否后的操作。
    ConnectionCallback connectionCallback = new ConnectionCallback() {
        @Override
        public void onServiceConnect() {
            isConnected = true;
            refreshQR();
        }

        @Override
        public void onServiceDisconnect() {
            isConnected = false;
        }
    };

    @Override
    public ProviderFormInfo bindFormData() {
        HiLog.info(TAG, "bind form data when create form");
        VisionManager.init(context, connectionCallback);

        ProviderFormInfo providerFormInfo = new ProviderFormInfo(RESOURCE_ID_MAP.get(dimension), context);
        return providerFormInfo;
    }

    private void refreshQR() {
        if (isConnected) {
            System.out.println(isConnected);
            PixelMap pm = QRUtils.createQR(context,"hello 你好啊");

            // 用于表示一个Java卡片实例
            ComponentProvider componentProvider = new ComponentProvider(RESOURCE_ID_MAP.get(dimension), context);
            componentProvider.setImagePixelMap(ResourceTable.Id_image, pm);

            try {
                ((MainAbility)context).updateForm(QRUtils.getFormId(), componentProvider);
            } catch (FormException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public void updateFormData(long formId, Object... vars) {
        HiLog.info(TAG, "update form data timing, default 30 minutes");

    }

    @Override
    public void onTriggerFormEvent(long formId, String message) {
        HiLog.info(TAG, "handle card click event.");
    }

    @Override
    public Class<? extends AbilitySlice> getRoutePageSlice(Intent intent) {
        HiLog.info(TAG, "get the default page to route when you click card.");
        return null;
    }
}